﻿using System;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
namespace Services.Maps
{
    public class BaiduMapTileLayer : TiledMapServiceLayer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="style"></param>
        public BaiduMapTileLayer(string style)
        {
            _mapStyle = style;
        }
        /// <summary>
        /// 
        /// </summary>
        public BaiduMapTileLayer()
            : this("Image")
        {
        }
        //成员变量 
        private TileInfo _tileInfo = new TileInfo();
        private int _wkid = 102100;
        private string _mapStyle;
        private double cornerCoordinate = 20037508.34;
        private Envelope _initialExtent = new Envelope(5916776.8, 1877209.3, 19242502.6, 7620381.8);
        /// <summary> </summary>
        public override void Initialize()
        {
            this.FullExtent = _initialExtent;
            this.SpatialReference = new SpatialReference(_wkid);

            this.TileInfo = new TileInfo()
            {
                Height = 256,
                Width = 256,
                Origin = new MapPoint(-cornerCoordinate, cornerCoordinate)
                {
                    SpatialReference = new SpatialReference(_wkid)
                },
                Lods = new Lod[17]
            };

            double resolution = 156543.033928;
            for (int i = 0; i < TileInfo.Lods.Length; i++)
            {
                TileInfo.Lods[i] = new Lod() { Resolution = resolution };
                resolution /= 2;
            }

            base.Initialize();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="level"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        public override string GetTileUrl(int level, int row, int col)
        {
            var zoom = level - 1;
            var offsetX = Math.Pow(2, zoom);
            var offsetY = offsetX - 1;
            var numX = col - offsetX;
            var numY = (-row) + offsetY;

            zoom = level + 1;
            var num = (col + row) % 8 + 1;

            var url = string.Empty;
            if (_mapStyle == "Vector") //获取矢量地图  
            {
                //http://online2.map.bdimg.com/tile/?qt=tile&x=209&y=77&z=10&styles=pl&udt=20140212
                url = "http://online" + num + ".map.bdimg.com/tile/?qt=tile&x=" + numX + "&y=" + numY + "&z=" + zoom + "&style=pl&udt=" + DateTime.Now.ToString("yyyyMMdd");
            }
            else if (_mapStyle == "Image") //影像地图
            {
                url = "http://q" + num + ".baidu.com/it/u=x=" + numX + ";y=" + numY + ";z=" + zoom + ";v=009;type=sate&fm=46";
            }
            else if (_mapStyle == "POI") //获取道路等POI，和影像地图配合使用  
            {
                url = "http://online" + num + ".map.bdimg.com/tile/?qt=tile&x=" + numX + "&y=" + numY + "&z=" + zoom + "&styles=sl&v=037&udt=20140922";
            }
            return url;
        }
    }
}